//绘制定子

var angle = 1;
var x=300,y=300,radio=200;
var anime_angle=0;

var pole_pairs = 2;     //极对数
var HallA = 0;
var HallB = 120;
var HallC = 240;
var HallA_Sta;
var HallB_Sta;
var HallC_Sta;

var rotateTimerHandler = 0;
var rotateSpeed = $("#Rspd").val();

$("#Rspd").on("change", function(){
    rotateSpeed = $("#Rspd").val();
});

$("#st_btn").on("click", function(){
    if($("#st_btn").text() == "开始旋转"){
        rotateSpeed = 100-$("#Rspd").val();
        rotateTimerHandler = setInterval(MotorRotate, rotateSpeed);
        $("#st_btn").text("停止旋转");
    }
    else{
        clearInterval(rotateTimerHandler);
        $("#st_btn").text("开始旋转");
    }
});

$("#polePairBtn").on("click",function(){
    var val = $("#polePairBtn").text();
    if(++val > 4){
        val = 1;
    }
    $("#polePairBtn").text(val);
    pole_pairs = $("#polePairBtn").text();
    MotorRotate(0);
});

$("#HallPosBtn").on("click",function(){
    HallA = parseInt($("#HA").val());
    HallB = parseInt($("#HB").val());
    HallC = parseInt($("#HC").val());
    console.log("霍尔角度"+"A:"+HallA+"  B:"+HallB+"  C:"+HallC);
    drawRotate(0);
});
$("#resetBtn").on("click",function(){
    anime_angle = 0;
    MotorRotate(0);
});
$("#recordTbBtn").on("click", function(){

});
$("#resetRecord").on("click",function(){
    $("#record").empty();
})

$("#HallSta").on("change",function(){
    console.log("chan");
    $("#record").append($("HallSta").val());
})

function showHallSta(A,B,C)
{
    let str = "HA:["+A+"]  HB:["+B+"]  HC:["+C+"]";
    $("#HallSta").val(str); 
}
function calcPosition(){
    let record_flag;
    let l_angle = (anime_angle+HallA)%360;  //计算位置偏移量
    let int = parseInt(l_angle/(360/pole_pairs/2));
    if(HallA_Sta != int%2){
        HallA_Sta = int%2;
        record_flag = true;
    }
    //console.log("H角"+HallA+"  转角"+anime_angle+"  计角:"+l_angle+"   "+int);

    l_angle = (anime_angle+360-HallB)%360;  //计算位置偏移量
    int = parseInt(l_angle/(360/pole_pairs/2));
    if(HallB_Sta != int%2){
        HallB_Sta = int%2;
        record_flag = true;
    }

    l_angle = (anime_angle+360-HallC)%360;  //计算位置偏移量
    int = parseInt(l_angle/(360/pole_pairs/2));
    if(HallC_Sta != int%2){
        HallC_Sta = int%2;
        record_flag = true;
    }
    if(record_flag)
    {
        $("#record").append("<br>"+$("#HallSta").val());
    }
    showHallSta(HallA_Sta, HallB_Sta, HallC_Sta);
}

setInterval(calcPosition, 100);
//弧度 = 角度 * Math.Pi/180;
//角度 = 弧度/Math.PI*180
//绘制电机转子：旋转角度
function drawRotate(rAngle){
    var cx = document.getElementById("test");
    var ctx = cx.getContext("2d");
    ctx.clearRect(0,0,600,600);
    init(ctx);
    drawHall(ctx, "A", HallA);
    drawHall(ctx, "B", HallB);
    drawHall(ctx, "C", HallC);
    //绘制霍尔传感器
    function drawHall(ctx, name, angle){
        ctx.save();
        ctx.translate(300,300);
        ctx.rotate(angle*Math.PI/180);
        ctx.translate(204,0);
        ctx.fillRect(0,-15,30,30);
        ctx.font = "20px sans-serif";
        ctx.fillStyle = "white";
        ctx.fillText(name,10,8);
        ctx.stroke();
        ctx.restore();
    }
    //绘制扇形(上下文， 弧度，旋转角度，颜色)
    function drawSector(ctx, rad, angle, color, txt){
        ctx.save();
        ctx.translate(300,300);
        ctx.rotate(Math.PI/180 * angle);
        ctx.beginPath();
        ctx.moveTo(0 ,0);
        ctx.lineTo(200, 0);
        ctx.arc(0, 0, 200, 0, rad*Math.PI/180, false);
        ctx.lineTo(0,0);
        ctx.fillStyle=color;
        ctx.fill();
        ctx.fillStyle = "white"
        ctx.font = "20px scan-serif"
        if(rad != 180){
            let y = Math.tan(rad/2*Math.PI/180)*110;
            //console.log(y);
            ctx.fillText(txt, 110, y+15);
        }
        else{
            //console.log("rad = 180度");
            let y = 130;
            ctx.fillText(txt, -15, 130);
        }
        ctx.stroke();
        ctx.closePath();
        ctx.restore();
    }  
    //初始化图形
    function init(ctx){
        //绘制圆形
        ctx.arc(300, 300, 200, 0, 2*Math.PI, false);
        ctx.strokeStyle="#F0F"
        ctx.stroke();
        ctx.closePath();
        ctx.restore();
        //绘制霍尔传感器
        ctx.arc(300, 300, 240, 0, 2*Math.PI, false);
        ctx.strokeStyle="#F0F"
        ctx.stroke();
        ctx.closePath();
        ctx.restore();
        //绘制坐标轴
        ctx.beginPath();
        ctx.moveTo(0,300);
        ctx.lineTo(800,300);
        ctx.moveTo(300,0);
        ctx.lineTo(300,800);
        ctx.strokeStyle="#F00";
        ctx.stroke(); 
        ctx.restore();
    }
    //计算位置
    //calcPosition();
    function calcPosition(){
        let angle = (anime_angle+HallA)%360;  //计算位置偏移量
        let int = parseInt(angle/90);
        HallA_Sta = int%2;
        angle = (anime_angle+HallB)%360;  //计算位置偏移量
        int = parseInt(angle/90);
        HallB_Sta = int%2;
        
        angle = (anime_angle+HallC)%360;  //计算位置偏移量
        int = parseInt(angle/90);
        HallC_Sta = int%2;

        showHallSta(HallA_Sta, HallB_Sta, HallC_Sta);
    }
    //绘制扇形
    let pole = pole_pairs*2;  //计算级数
    for(i=0; i<pole; i++){
        let angle = 360/pole*i; 
        drawSector(ctx, 360/pole, angle+rAngle, (i%2?"blue":"red"), i+1);
    }
}
//旋转一圈按钮
$("#st_one_btn").on("click",function(){

});
function MotorRotateCircle()
{

}
//重绘图：旋转角度
function MotorRotate(angle)
{
    if(anime_angle>360)
    {
        anime_angle =0;
        $("#record").append("<br>"+"旋转一圈");
    }
    anime_angle += 0.5;
    drawRotate(anime_angle);
}
//drawRotate(5);
MotorRotate(0);
//setInterval(MotorRotate, 20);
